package com.sduept.i1;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.ssssssss.magicapi.Const;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.core.intf.Packet;
import org.tio.server.intf.TioServerListener;
import com.sduept.i1.model.MonitorPacket;
public class I1ServerListener implements TioServerListener{
	private static final Logger logger = LoggerFactory.getLogger(I1ServerListener.class);
	
	@Override
	public void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect)
			throws Exception {
		logger.info("--------------------------I1ServerListener.onAfterConnected");
		logger.info("有新连接：{}",channelContext.getBsId());
	}

	@Override
	public void onAfterDecoded(ChannelContext channelContext, Packet packet, int packetSize) throws Exception {
		logger.info("---------------------------I1ServerListener.onAfterDecoded");
		
		// TODO Auto-generated method stub
		String bsId = ((MonitorPacket)packet).identification;
        ChannelContext searchChannelContext = Tio.getByBsId(I1Server.getInstance().getServerTioConfig(), bsId);
        if(searchChannelContext != null) {
            if(searchChannelContext == channelContext) { //找到上次连接，并且与形参连接一致
            } else { //找到上次连接，与形参连接不一致，则关闭上次连接,新建packet
                Tio.close(searchChannelContext, "每个设备只保留最新的连接");
                Tio.bindBsId(channelContext, bsId);
                logger.info("绑定连接：{}",bsId);
            }
        } else { //未找到上次连接,新建packet
            Tio.bindBsId(channelContext, bsId);
            logger.info("绑定连接：{}",bsId);
        }
        if(I1Server.bDebug) {
            // 数据处理，调用magic-api接口
            Map<String,Object> params = new HashMap<>();
            params.put("bytes",packet.toString());
            params.put("info","I1协议-----解析包");
            params.put("time",Const.df.format(new Date()));
            Const.getService().execute("POST","/network/revDebugData", params);
        }
	}

	@Override
	public void onAfterReceivedBytes(ChannelContext channelContext, int receivedBytes) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAfterSent(ChannelContext channelContext, Packet packet, boolean isSentSuccess) throws Exception {
        if(I1Server.bDebug) {
            // 数据处理，调用magic-api接口
            Map<String,Object> params = new HashMap<>();
            params.put("bytes",packet.toString());
            params.put("info","I1协议-----发送包");
            params.put("time",Const.df.format(new Date()));
            Const.getService().execute("POST","/network/revDebugData", params);
        }		
	}

	@Override
	public void onAfterHandled(ChannelContext channelContext, Packet packet, long cost) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onBeforeClose(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onHeartbeatTimeout(ChannelContext channelContext, Long interval, int heartbeatTimeoutCount) {
		// TODO Auto-generated method stub
		return false;
	}

}
